import { isDevMode } from '../env/env';
import { useGlobSetting } from '../../hooks/setting/index';

export function transformEnvToDev(
  url: string,
  businessUiPort?: number,
  businessUiEnName?: string,
  openType: 'open' | 'replace' = 'open'
) {
  /**
   * @description: judge if it is dev mode and transform url to devUrl
   */
  const { appEnName, devPort } = useGlobSetting();
  const port = businessUiPort ? businessUiPort : devPort;
  const enName = businessUiEnName ? businessUiEnName : appEnName;
  const isDev = isDevMode();
  if (isDev) {
    const tmp = url.split('?')[1];
    url = `https://localhost:${port}/${enName}/media?${tmp}`;
  }
  if (openType === 'open') {
    window.open(url, '_blank');
  } else {
    window.location.replace(url);
  }
}
